import echart from 'echarts'

var echartsObj = {};

/**
 * @export 生成x轴
 * @param {string} name x轴单位（例如：辆）
 * @param {object} data 对应x轴的显示数据 （例如：[1,2,3]）
 * @param {boolean} isFirst 是否贴边(与边上有距离没) (默认为 false)
 * @returns 
 */
echartsObj.getXAxis = function(name, data, isFirst) {
    var _isFirst = (isFirst != undefined) ? isFirst : false;

    var xAxis = {
        type: 'category', // category坐标轴类型，横轴默认为类目轴，数值轴则参考yAxis说明
        name: name,
        // nameGap: -4,
        boundaryGap: _isFirst, //是否贴边
        symbol: {

        },
        axisLine: {
            show: true, //y轴的线
            symbol: ['none', 'arrow'],
            symbolSize: [4, 6],
            symbolOffset: [12, 5.5],
            lineStyle: {
                width: 1,
                color: 'rgba(145,208,242,0.5)',
                type: 'solid'
            }
        },
        axisTick: {
            show: false
        },
        axisLabel: {
            show: true,
            interval: null,
            margin: 2,
            textStyle: {
                interval: null,
                fontSize: 10, //x轴文字大小
                color: 'rgba(145,208,242)' //x轴文字颜色
            }
        },
        splitLine: {
            show: false, //分隔线
            lineStyle: {
                color: 'rgba(145,208,242,0.5)' //分隔线颜色
            }
        },
        splitArea: {
            show: false //分隔区域
        },
        data: data
    }
    return xAxis
}

/**
 * @export 生成y轴
 * @param {string} name y轴单位（例如：辆）
 * @param {object} data 对应y轴的显示数据 （例如：[1,2,3]）
 * @returns     
 */
echartsObj.getYAxis = function(name, data) {
    var yAxis = {
        type: 'value',
        name: name,
        nameTextStyle: {
            color: 'rgba(145,208,242,0.3)',
            fontSize: 10
        },
        minInterval: 1,
        splitNumber: 5,

        axisLine: {
            show: true, //y轴的线
            symbol: ['none', 'arrow'],
            symbolSize: [4, 6],
            symbolOffset: [6, 6],
            lineStyle: {
                width: 1,
                color: 'rgba(145,208,242,0.5)',
                type: 'solid'
            }
        },
        axisTick: {
            show: false
        },
        axisLabel: {
            show: true,
            margin: 20,
            textStyle: {
                fontSize: 10, //y轴文字大小
                color: 'rgba(145,208,242)'
            }
        },
        splitLine: {
            show: false, //分隔线
            lineStyle: {
                color: 'rgba(145,208,242,0.5)'
            }
        },
        splitArea: {
            show: false //分隔区域
        },
        data: data
    }
    return yAxis
}

/**
 * @export 生成曲线对应series（详情框）
 * @param {string} name series对应的显示单位
 * @returns     
 */
echartsObj.getLineSeries = function(name, data, _lineColor, _itemColor) {
    var series = {
        type: 'line',
        data: data,
        name: name,
        symbol: 'emptyCircle',
        smooth: true,
        barWidth: 14,
        areaStyle: {},
        lineStyle: {
            borderWidth: 1,
            normal: {
                color: _lineColor
            }
        },
        itemStyle: {
            normal: {
                barBorderRadius: 0,
                color: _itemColor,
                shadowColor: 'rgba(0, 0, 0, 0.2)',
                shadowBlur: 20
            }
        }
    }
    return series
}

/**
 * unit 单位 "座"
 * radis 半径 "70%""
 * center 位置 ["50%", "50%"]
 * legendData 图例数据
 * pieData 扇形数据
 */
echartsObj.getPieService = function(unit, radius, center, legendData, pieData) {
    var pieService = {
        name: unit,
        type: "pie",
        radius: radius,
        center: center,
        hoverOffset: 5,
        label: {
            color: '#91d0f2',
            fontSize: 12,
            padding: 6,
            backgroundColor: 'rgba(16,52,74,0.72)',
            borderColor: '#477B97',
            borderWidth: 1,
            borderRadius: 2,
            formatter: '{d}%'
        },
        labelLine: {
            length: 10,
            length2: 24,
            lineStyle: {
                color: '#477B97'
            }
        },
        data: echartSeries(legendData, pieData),
        itemStyle: {
            emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)"
            }
        }

    }

    function echartSeries(legendData, data) {
        let _data = [];
        let _colorDataFrom = [
            new echart.graphic.LinearGradient(0, 0, 0, 1, [{
                offset: 0,
                color: '#3CADFB'
            }, {
                offset: 1,
                color: '#366BC0'
            }]),
            new echart.graphic.LinearGradient(0, 0, 0, 1, [{
                offset: 0,
                color: '#FBDC6C'
            }, {
                offset: 1,
                color: '#F38242'
            }]),
            new echart.graphic.LinearGradient(0, 0, 0, 1, [{
                offset: 0,
                color: '#3ADA7D'
            }, {
                offset: 1,
                color: '#1D834E'
            }]),
            new echart.graphic.LinearGradient(0, 0, 0, 1, [{
                offset: 0,
                color: '#7F64D8'
            }, {
                offset: 1,
                color: '#543D9F'
            }]),
            new echart.graphic.LinearGradient(0, 0, 0, 1, [{
                offset: 0,
                color: '#D969F6'
            }, {
                offset: 1,
                color: '#9A39B3'
            }]),
            new echart.graphic.LinearGradient(0, 0, 0, 1, [{
                offset: 0,
                color: '#C3F964'
            }, {
                offset: 1,
                color: '#73BD55'
            }]),
        ];
        for (let i = 0; i < legendData.length; i++) {
            let simData = {
                itemStyle: {
                    normal: {
                        barBorderRadius: [0, 10, 10, 0],
                        color: _colorDataFrom[i],
                        shadowColor: "rgba(0, 0, 0, 0.4)",
                        shadowBlur: 20
                    }
                },
                value: data[i],
                name: legendData[i].name
            }

            _data.push(simData);
        }
        return _data;
    }
    return pieService;
}

export default echartsObj;